/*
 * Decompiled with CFR 0.150.
 * 
 * Could not load the following classes:
 *  cn.hutool.core.util.ObjectUtil
 *  com.alibaba.fastjson.JSON
 *  de.felixroske.jfxsupport.FXMLController
 *  javafx.beans.value.ChangeListener
 *  javafx.beans.value.ObservableValue
 *  javafx.collections.FXCollections
 *  javafx.collections.ObservableList
 *  javafx.event.EventHandler
 *  javafx.fxml.FXML
 *  javafx.fxml.Initializable
 *  javafx.scene.Node
 *  javafx.scene.control.Alert
 *  javafx.scene.control.Alert$AlertType
 *  javafx.scene.control.CheckBox
 *  javafx.scene.control.ScrollPane
 *  javafx.scene.control.TableCell
 *  javafx.scene.control.TableColumn
 *  javafx.scene.control.TableView
 *  javafx.scene.control.TextField
 *  javafx.scene.control.cell.PropertyValueFactory
 *  javafx.scene.input.KeyCode
 *  javafx.scene.input.KeyEvent
 *  javafx.scene.layout.VBox
 *  javafx.stage.Modality
 *  javafx.util.Callback
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 *  org.springframework.beans.factory.annotation.Autowired
 *  org.springframework.util.CollectionUtils
 *  org.springframework.util.StringUtils
 */
package com.gys.controller.member;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.gys.GysApplication;
import com.gys.common.business.MemberAddOrEditUtil;
import com.gys.common.data.HttpJson;
import com.gys.common.util.CommonUtil;
import com.gys.controller.BaseController;
import com.gys.controller.dataManger.ControllerManger;
import com.gys.controller.index.IndexController;
import com.gys.controller.member.MemberInfoController;
import com.gys.data.member.GetMemberInData;
import com.gys.data.member.GetMemberOutData;
import com.gys.view.member.MemberInfoView;
import de.felixroske.jfxsupport.FXMLController;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.ResourceBundle;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Alert;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.util.Callback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

@FXMLController
public class MemberAddOrEditController
extends BaseController
implements Initializable {
    private static final Logger log = LoggerFactory.getLogger(MemberAddOrEditController.class);
    @FXML
    private TextField gmbNameSearch;
    @FXML
    private TextField gmbTelSearch;
    @FXML
    private TextField gmbMobileSearch;
    @FXML
    private TextField idCard;
    @FXML
    private TextField gmbCredentialsSearch;
    @FXML
    private TableView<GetMemberOutData> dataTable;
    @FXML
    private TableColumn<GetMemberOutData, Integer> index;
    @FXML
    private TableColumn<GetMemberOutData, CheckBox> memberCheckBox;
    @FXML
    private TableColumn<GetMemberOutData, String> gmbUpdateDate;
    @FXML
    private TableColumn<GetMemberOutData, String> gmbCardId;
    @FXML
    private TableColumn<GetMemberOutData, String> gmbClassId;
    @FXML
    private TableColumn<GetMemberOutData, String> gmbStatus;
    @FXML
    private TableColumn<GetMemberOutData, String> clientId;
    @FXML
    private TableColumn<GetMemberOutData, String> gmbBrId;
    @FXML
    private TableColumn<GetMemberOutData, String> gmbBrName;
    @FXML
    private TableColumn<GetMemberOutData, String> gmbBrId2;
    @FXML
    private TableColumn<GetMemberOutData, String> gmbContactAllowed;
    @FXML
    private TableColumn<GetMemberOutData, String> gmbIntegral;
    @FXML
    private TableColumn<GetMemberOutData, String> gmbChannel;
    @FXML
    private ScrollPane scrollPane;
    @FXML
    private VBox vbox;
    @Autowired
    private IndexController indexController;
    @Autowired
    private MemberInfoController memberInfoController;
    private ObservableList<GetMemberOutData> observableTableList = FXCollections.observableArrayList(new ArrayList());
    public GetMemberOutData memberData = null;
    public String cardStatus = null;

    public void initialize(URL location, ResourceBundle resources) {
        ControllerManger.controllerMap.put(this.getClass().getSimpleName(), this);
        this.scrollPane.setPrefWidth(this.getContentWidth());
        this.scrollPane.setPrefHeight(this.getContentHeight());
        this.vbox.setPrefHeight(this.getContentHeight());
        this.vbox.setPrefWidth(this.getContentWidth());
        this.dataTable.setPrefWidth(this.getContentWidth() * 0.95);
        this.dataTable.setPrefHeight(this.getContentHeight() * 0.65);
        this.bangding();
        this.initMemberCheckBox();
        this.initSelectTable();
        this.init();
    }

    @Override
    public void init() {
        if (ObjectUtil.isEmpty(this.scrollPane)) {
            return;
        }
        this.indexController.scrollContentPane.setOnKeyReleased((EventHandler)new EventHandler<KeyEvent>(){

            public void handle(KeyEvent event) {
                if (event.getCode() == KeyCode.F1) {
                    MemberAddOrEditController.this.addBtnAction();
                } else if (event.getCode() == KeyCode.ENTER) {
                    MemberAddOrEditController.this.searchBtnAction();
                } else if (event.getCode() == KeyCode.F3) {
                    MemberAddOrEditController.this.editBtnAction();
                }
            }
        });
        this.cardStatus = "2";
        this.initTable();
    }

    private void initSelectTable() {
        this.dataTable.getSelectionModel().selectedItemProperty().addListener((ChangeListener)new ChangeListener<GetMemberOutData>(){

            public void changed(ObservableValue<? extends GetMemberOutData> observableValue, GetMemberOutData oldItem, GetMemberOutData newItem) {
                if (ObjectUtil.isEmpty(newItem)) {
                    return;
                }
                newItem.getMemberCheckBox().setSelectStatus(true);
                MemberAddOrEditController.this.memberData = newItem;
                for (int i = 0; i < MemberAddOrEditController.this.dataTable.getItems().size(); ++i) {
                    GetMemberOutData getMemberOutData = (GetMemberOutData)MemberAddOrEditController.this.dataTable.getItems().get(i);
                    if (getMemberOutData.getIndex().equals(newItem.getIndex())) continue;
                    getMemberOutData.getMemberCheckBox().setSelectStatus(false);
                }
            }
        });
    }

    private void initMemberCheckBox() {
        this.memberCheckBox.setCellFactory(col -> {
            TableCell<GetMemberOutData, CheckBox> cell = new TableCell<GetMemberOutData, CheckBox>(){

                protected void updateItem(CheckBox item, boolean empty) {
                    super.updateItem(item, empty);
                    this.setGraphic((Node)item);
                    if (item != null) {
                        item.setOnMouseClicked(col -> {
                            if (CollectionUtils.isEmpty((Collection)MemberAddOrEditController.this.observableTableList)) {
                                return;
                            }
                            int index = this.getIndex();
                            for (int i = 0; i < MemberAddOrEditController.this.observableTableList.size(); ++i) {
                                if (i == index) continue;
                                ((GetMemberOutData)MemberAddOrEditController.this.observableTableList.get(i)).getMemberCheckBox().setSelectStatus(false);
                            }
                            MemberAddOrEditController.this.memberData = ((GetMemberOutData)MemberAddOrEditController.this.observableTableList.get(index)).getMemberCheckBox().isSelected() != false ? (GetMemberOutData)MemberAddOrEditController.this.observableTableList.get(index) : null;
                        });
                    }
                }
            };
            return cell;
        });
    }

    public TableView<GetMemberOutData> initTable() {
        this.memberData = null;
        GetMemberInData inData = new GetMemberInData();
        inData.setGmbName(this.gmbNameSearch.getText().trim());
        inData.setGmbTel(this.gmbTelSearch.getText().trim());
        inData.setGmbMobile(this.gmbMobileSearch.getText().trim());
        inData.setGmbCredentials(this.gmbCredentialsSearch.getText().trim());
        inData.setGmbStatus(this.cardStatus);
        inData.setGmbCardId(this.idCard.getText());
        List outDataList = null;
        HttpJson httpJson = MemberAddOrEditUtil.findMembersByPage(CommonUtil.convertToMap(inData));
        if (!httpJson.isSuccess()) {
            Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", httpJson.getMsg());
            alert.showAndWait();
            return new TableView();
        }
        outDataList = JSON.parseArray((String)httpJson.getMsg(), GetMemberOutData.class);
        this.observableTableList.clear();
        this.observableTableList = FXCollections.observableArrayList((Collection)outDataList);
        this.dataTable.setItems(this.observableTableList);
        return this.dataTable;
    }

    private void bangding() {
        this.memberCheckBox.setCellValueFactory(cellData -> ((GetMemberOutData)cellData.getValue()).getMemberCheckBox().getCustomCheckBox());
        this.index.setCellValueFactory((Callback)new PropertyValueFactory("index"));
        this.gmbUpdateDate.setCellValueFactory((Callback)new PropertyValueFactory("gmbUpdateDate"));
        this.gmbCardId.setCellValueFactory((Callback)new PropertyValueFactory("gmbCardId"));
        this.gmbClassId.setCellValueFactory((Callback)new PropertyValueFactory("gmbClassId"));
        this.gmbStatus.setCellValueFactory((Callback)new PropertyValueFactory("gmbStatus"));
        this.clientId.setCellValueFactory((Callback)new PropertyValueFactory("clientId"));
        this.gmbBrId.setCellValueFactory((Callback)new PropertyValueFactory("gmbBrId"));
        this.gmbBrName.setCellValueFactory((Callback)new PropertyValueFactory("gmbBrName"));
        this.gmbBrId2.setCellValueFactory((Callback)new PropertyValueFactory("gmbCardId"));
        this.gmbContactAllowed.setCellValueFactory((Callback)new PropertyValueFactory("gmbContactAllowed"));
        this.gmbIntegral.setCellValueFactory((Callback)new PropertyValueFactory("gmbIntegral"));
        this.gmbChannel.setCellValueFactory((Callback)new PropertyValueFactory("gmbChannel"));
    }

    @FXML
    private void addBtnAction() {
        this.cardStatus = "1";
        this.initTable();
    }

    @FXML
    private void searchBtnAction() {
        String gmbNameSearchStr = this.gmbNameSearch.getText().trim();
        String gmbTelSearchStr = this.gmbTelSearch.getText().trim();
        String gmbMobileSearchStr = this.gmbMobileSearch.getText().trim();
        String gmbCredentialsSearchStr = this.gmbCredentialsSearch.getText().trim();
        String idCardStr = this.idCard.getText().trim();
        Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", null);
        if (StringUtils.isEmpty(gmbNameSearchStr) && StringUtils.isEmpty(gmbTelSearchStr) && StringUtils.isEmpty(gmbMobileSearchStr) && StringUtils.isEmpty(gmbCredentialsSearchStr) && StringUtils.isEmpty(idCardStr)) {
            alert.setContentText("\u5fc5\u987b\u81f3\u5c11\u586b\u5199\u4e00\u9879\u5185\u5bb9\u8fdb\u884c\u67e5\u8be2\uff01");
            alert.showAndWait();
            return;
        }
        this.initTable();
    }

    @FXML
    private void backAction() {
        this.cardStatus = "2";
        this.initTable();
    }

    @FXML
    private void editBtnAction() {
        if (ObjectUtil.isEmpty(this.memberData)) {
            Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", "\u8bf7\u9009\u62e9\u8bb0\u5f55");
            alert.showAndWait();
            return;
        }
        this.memberInfoController.init();
        GysApplication.showView(MemberInfoView.class, (Modality)Modality.APPLICATION_MODAL);
    }

    @FXML
    private void restquery() {
        this.gmbNameSearch.clear();
        this.gmbTelSearch.clear();
        this.gmbMobileSearch.clear();
        this.gmbCredentialsSearch.clear();
    }
}

